package com.simon.viewlib;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Simon_Li1 on 1/11/2017.
 */

public class ArcProgressbar extends View {

    private static final int DEFAULT_STROKEWIDTH = 20;
    private int bgStrokeWidth = DEFAULT_STROKEWIDTH;
    private int barStrokeWidth = DEFAULT_STROKEWIDTH;
    private int bgColor = Color.GRAY;
    private int barColor = Color.GREEN;
    private int progress = 0;
    private int startAngle = 270;
    private int sweepAngle = 100;
    private Paint mPaintBar = null;

    private Paint mPaintBg = null;
    private RectF rectBg = null;
    /**
     * 直徑。
     */
    private int diameter = 200;
    public ArcProgressbar(Context context) {
        super(context);
    }

    public ArcProgressbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        init(canvas);
        invalidate();
    }

    public void init(Canvas canvas){

        // 画弧形的矩阵区域。
        rectBg = new RectF(5, 5, diameter, diameter);

        // 弧形背景。
        mPaintBg = new Paint();
        mPaintBg.setAntiAlias(true);
        mPaintBg.setStyle(Paint.Style.STROKE);
        mPaintBg.setStrokeWidth(bgStrokeWidth);
        mPaintBg.setStrokeCap(Paint.Cap.ROUND);
        mPaintBg.setColor(bgColor);
        canvas.drawArc(rectBg, startAngle, sweepAngle, false, mPaintBg);

        // 弧形ProgressBar。
        mPaintBar = new Paint();
        mPaintBar.setAntiAlias(true);
        mPaintBar.setStyle(Paint.Style.STROKE);
        mPaintBar.setStrokeWidth(barStrokeWidth);
        mPaintBar.setStrokeCap(Paint.Cap.ROUND);
        mPaintBar.setColor(barColor);
        canvas.drawArc(rectBg, startAngle, progress, false, mPaintBar);
    }

    public void setDiameter(int diameter){
        this.diameter = diameter;
        invalidate();
    }

    public void setProgress(int progress){
        this.progress = progress;
        if (progress > sweepAngle){
            progress = sweepAngle;
        }
        invalidate();
    }
}
